package com.ming.sorting.algorithm;

import java.util.Arrays;

/*
插入排序
 */
public class InsertSort {
    public static void main(String[] args) {
        int[] arr = new int[80000];

        for (int i = 0; i < 80000; i++) {
            arr[i]=(int)(Math.random()*8000000);
        }
        long l = System.currentTimeMillis();
        insertSort(arr);
        long l1 = System.currentTimeMillis();
//        System.out.println(l1-l);
        System.out.println(Arrays.toString(arr));
    }

    private static void insertSort(int[] arr) {//2237
        for (int i = 1; i < arr.length; i++) {
            int index = i;
            while (index >= 1 && arr[index] < arr[index - 1]) {
                arr[index] ^= arr[index - 1];
                arr[index - 1] ^= arr[index];
                arr[index] ^= arr[index - 1];
                index--;
            }
        }
    }

    private static void insertSort2(int[] arr) {//668
        for (int i = 1; i < arr.length; i++) {
            int index = i;
            int num=arr[index];
            while (index >= 1 && num < arr[index - 1]) {
                arr[index]=arr[index-1];
                index--;
            }
//            if (index!=i)
                arr[index]=num;
        }
    }
}
